package cn.niit.letuseatdemo.service.impl;

import cn.niit.letuseatdemo.entity.domain.SysUser;
import cn.niit.letuseatdemo.entity.domain.User;
import cn.niit.letuseatdemo.entity.domain.UserAddress;
import cn.niit.letuseatdemo.mapper.UserMapper;
import cn.niit.letuseatdemo.service.UserService;
import cn.niit.letuseatdemo.util.H5LoginUtil;
import cn.niit.letuseatdemo.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: olivia_jojo
 * @CreateTime： 2022/10/25 22:46
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /*
    * @Description: 根据用户的电话和密码查询，用于用户登录
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/10/31 0:37
     * @param: userName
     * @param: userPass
     * @return: cn.niit.letuseatdemo.entity.domain.User
    **/
    @Override
    public User getUserByUserTelPassword(String userTel, String userPass) {
        return userMapper.selectUserByUserTelPassword(userTel,userPass);
    }

    /**
     * 查询用户
     *
     * @param userId 用户主键
     * @return 用户
     */
    @Override
    public User selectUserByUserId(Long userId) {

        return userMapper.selectUserByUserId(userId);//
    }

    /**
     * 查询用户列表
     *
     * @param user 用户
     * @return 用户集合
     */
    @Override
    public List<User> selectUserList(User user) {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户
     *
     * @param user 用户
     * @return 结果
     */
    @Override
    public int insertUser(User user) {
        return 0;
    }

    /**
     * 修改用户
     *
     * @param user 用户
     * @return 结果
     */

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的用户主键集合
     * @return 结果
     */
    @Override
    public int deleteUserByUserIds(String userIds) {
        return 0;
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户主键
     * @return 结果
     */
    @Override
    public int deleteUserByUserId(Long userId) {
        return 0;
    }

   /*
   * @Description:更新用户信息
   * @Author: Jin_Na
   * @CreateTime: 2022/12/8 19:33
    * @param: user
    * @return: int
   **/
    @Override
    public int updateUser(User user){
        /** 获取当前登录用户**/
        SysUser loginUser = LoginUserUtil.getLoginSysUser();
//        User user = H5LoginUtil.getLoginUser();
        user.setUpdateUserId(user.getUpdateUserId());
        user.setUpdateUserName(user.getUserName());
        user.setGmtUpdate(new Date());
        return userMapper.updateUser(user);
    }

    /*
    * @Description: 注册用户
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/11/10 23:40
     * @param: user
     * @return: boolean
    **/
    @Override
    public boolean register(User user) {

        user.setDelFlag(0);
        user.setCreateUserId(0L);
        user.setCreateUserName(user.getUserName());
        user.setGmtCreate(new Date());
        int rows = userMapper.insertUser(user);
        if (rows ==1){
            return  true;
        }
        return false;
    }

    /*
    * @Description: 用户登录的方法
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/11/11 21:26
     * @param: user
     * @return: boolean
    **/
    @Override
    public User login(User user) {
        user = userMapper.selectUserByUserPhonePassword(user.getUserTel(), user.getUserPassword());
        return user;
//        if(null == user){   //账号密码不正确
//            return false;
//        }
//        //账号密码正确
//        return true;
    }

    /*
    * @Description: 全局获取user的登录信息
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/1 10:17
     * @param: user
     * @return: cn.niit.letuseatdemo.entity.domain.User
    **/
    @Override
    public User getUserInfo(User user) {
        user = H5LoginUtil.getLoginUser();
        user.getUserTel();
        user.getUserName();
        user.getUserBirthday();
        user.getUserEmail();

        return user;
    }



}
